import {
	BASE_URL
} from '@/config/config.js';
export function initData() {
	//this.initAppVersion();
	//this.initLabels();
	// #ifdef H5
	   uni.setStorageSync('playerSet',1)
	// #endif
	initCates();
	initFeedBackType();	
}
/**
 * // 初始化视频分类
 */
export function initLabels() {
	let labelList = uni.getStorageSync('VodLabelList')
	if (!labelList) {
		// get请求
		uni.$u.http.get("/type/labels").then((res) => {
			if (res) {
				labelList = res;
				uni.setStorageSync('VodLabelList',res);
			}
		}).catch(() => {

		})
	}
	return res;

}
/**
 * 初始化反馈类型
 */
export function initFeedBackType() {

	let feedBackTypeList = uni.getStorageSync('FeedBackTypeList')
	if (!feedBackTypeList) {
		uni.$u.http.get("/advise/type").then((res) => {
			if (res) {
				feedBackTypeList = res;
				uni.setStorageSync('FeedBackTypeList',res);
			}
		}).catch(() => {

		})
	}
	return feedBackTypeList;

}
/**
 * 初始化视频分类所有
 */
export function initCates() {
	let vodCatesList = uni.getStorageSync('VodCatesList')
	if (!vodCatesList) {
		uni.$u.http.get("/type/cates").then((res) => {
			if (res) {
				vodCatesList = res;
				uni.setStorageSync('VodCatesList',res);
			}
		})
	}
	let navList = [];
	vodCatesList.forEach((item) => {
		let nav = {
			"name": item.type.typeName,
			"typeId": item.type.typeId
		};
		navList.push(nav);
		let subNavList =[];
		item.childTypes.forEach((t) => {
			let subNav = {
				"name": t.typeName,
				"typeId": t.typeId,
				"typePid":  item.type.typeId
			}
			
			subNavList.push(subNav);
		})
		uni.setStorageSync('SubNavList'+item.type.typeId,subNavList);
	})
	uni.setStorageSync('NavList',navList);
	uni.setStorageSync('SubNavList0',navList);
	return vodCatesList;
}


export function mycache(key, value, seconds = 3600 * 24) {
	let nowTime = Date.parse(new Date()) / 1000;
	if (key && value) {
		let expire = nowTime + Number(seconds);
		uni.setStorageSync(key, JSON.stringify(value) + '|' + expire)
		console.log('已经把' + key + '存入缓存,过期时间为' + expire)
	} else if (key && !value) {
		let val = uni.getStorageSync(key);
		if (val) {
			// 缓存存在，判断是否过期
			let temp = val.split('|')
			if (!temp[1] || temp[1] <= nowTime) {
				uni.removeStorageSync(key)
				console.log(key + '缓存已失效')
				return '';
			} else {
				return JSON.parse(temp[0]);
			}
		}
	}

}
export function checkAndUpdate(flag) {
	// #ifdef APP-PLUS
	let data = {
		appId: plus.runtime.appid,
		version: plus.runtime.version
	}
	console.info(data)
	uni.request({
		url: BASE_URL + "/app/checkUpdate", //请求更新地址
		data: data,
		success(res) {
			console.info(res.data.code)
			if (res.data.code == "200" && res.data.data) {
				uni.showModal({
					title: '检测到新版本' + res.data.data.version,
					content: res.data.data.note,
					confirmText: "更新",
					success: function(e) {
						if (e.confirm) {
							if (plus.os.name.toLowerCase() == 'ios') {
								// 跳转到下载页面
								plus.runtime.openURL(res.data.data.url)
							} else {
								var dtask = plus.downloader.createDownload(
									res.data.data.url, {},
									function(d, status) {
										uni.showToast({
											title: '下载完成',
											mask: false,
											duration: 1000
										});
										// 下载完成
										if (status == 200) {
											plus.runtime.install(plus.io
												.convertLocalFileSystemURL(d.filename), {},
												e => e,
												function(error) {
													uni.showToast({
														title: '安装失败-01',
														mask: false,
														duration: 1500
													});
												})
										} else {
											uni.showToast({
												title: '更新失败-02',
												mask: false,
												duration: 1500
											});
										}
									});
								try {
									dtask.start(); // 开启下载的任务
									var prg = 0;
									var showLoading = plus.nativeUI.showWaiting(
										"正在下载"); //创建一个showWaiting对象 
									dtask.addEventListener('statechanged', function(
										task,
										status
									) {
										// 给下载任务设置一个监听 并根据状态  做操作
										switch (task.state) {
											case 1:
												showLoading.setTitle("正在下载");
												break;
											case 2:
												showLoading.setTitle("已连接到服务器");
												break;
											case 3:
												prg = parseInt(
													(parseFloat(task.downloadedSize) /
														parseFloat(task.totalSize)) *
													100
												);
												showLoading.setTitle("  正在下载" + prg +
													"%  ");
												break;
											case 4:
												plus.nativeUI.closeWaiting();
												//下载完成
												break;
										}
									});
								} catch (err) {
									plus.nativeUI.closeWaiting();
									uni.showToast({
										title: '更新失败-03',
										mask: false,
										duration: 1500
									});
								}
							}

						} else {
							//取消
						}
					}
				});
			} else {
				if (flag) {
					uni.showModal({
						title: '提示',
						content: '已是最新版本',
						showCancel: false
					});
				}
			}
		},
	})
	// #endif
}
